package cn.itcast.web.controller.cargo;

import cn.itcast.domain.shipping.Shipping;
import cn.itcast.domain.shipping.ShippingExample;
import cn.itcast.service.cargo.ShippingService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.BeanMapUtils;
import cn.itcast.web.utils.DownloadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import net.sf.jasperreports.engine.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cargo/shipping")
public class OutShippingController extends BaseController {

    @Reference(check = false,retries = 0)
    private ShippingService shippingService;

    @RequestMapping("/printExcel")
    @ResponseBody
    public void printExcel() throws IOException {
        //1. 创建工作薄
        Workbook workbook = new XSSFWorkbook();
        //2. 创建工作单
        Sheet sheet = workbook.createSheet("委托单");
        //3. 合并单元格
        sheet.addMergedRegion(new CellRangeAddress(0,0,1,22));

        //4. 设置列宽
        sheet.setColumnWidth(0,6*512);
        sheet.setColumnWidth(1,21*256);
        sheet.setColumnWidth(2,16*256);
        sheet.setColumnWidth(3,29*256);
        for (int i = 4; i <=22; i++) {
            sheet.setColumnWidth(i,11*256);
        }

        //5. 创建第一行，并且得到标题内容
        Row row = sheet.createRow(0);
        String title  = getLoginCompanyName()+"委托单表";
        //创建单元格
        Cell cell = row.createCell(1);
        //设置标题的样式
        cell.setCellStyle(bigTitle(workbook));
        //设置单元格的内容
        cell.setCellValue(title);

        //6. 创建第二行，设置列头的内容
        row = sheet.createRow(1);
        String[] titles = {"编号","运输方式","货主","提单抬头","正本通知人","信用证","装运港","转船港","卸船港","装期","效期","是否分批","是否转船","份数","扼要说明","运输要求","运费说明","复核人","状态","创建人","创建部门","创建日期"};
        for (int i = 0; i < titles.length; i++) {
            //创建单元格
            cell = row.createCell(i+1);
            //设置单元格的样式
            cell.setCellStyle(title(workbook));
            //设置单元格的值
            cell.setCellValue(titles[i]);
        }
        //7. 查询委托单表的数据，然后遍历设置到excel中
        List<Shipping> shippingList = shippingService.findAll(new ShippingExample());

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        //定义一个变量记录当前行数，数据部分是索引为2开始
        int index = 2;
        for (Shipping shipping : shippingList) {
            //创建行
            row = sheet.createRow(index++);

            //编号
            String shippingOrderId = shipping.getShippingOrderId();
            if(shippingOrderId!=null){
                cell = row.createCell(1);
                //设置样式
                cell.setCellStyle(text(workbook));
                //设置内容
                cell.setCellValue(shippingOrderId);
            }

            //运输方式
            String orderType = shipping.getOrderType();
            if(orderType!=null){
                cell = row.createCell(2);
                //设置样式
                cell.setCellStyle(text(workbook));
                //设置内容
                cell.setCellValue(orderType);
            }

            //货主
            String shipper = shipping.getShipper();
            if(shipper!=null){
                cell = row.createCell(3);
                //设置样式
                cell.setCellStyle(text(workbook));
                //设置内容
                cell.setCellValue(shipper);
            }

            //提单抬头
            String consignee = shipping.getConsignee();
            if(consignee!=null){
                cell = row.createCell(4);
                //设置样式
                cell.setCellStyle(text(workbook));
                //设置内容
                cell.setCellValue(consignee);
            }

            //正本通知人
            String notifyParty = shipping.getNotifyParty();
            if(notifyParty!=null){
                cell = row.createCell(5);
                //设置样式
                cell.setCellStyle(text(workbook));
                //设置内容
                cell.setCellValue(notifyParty);
            }

            //信用证
            String lcNo = shipping.getLcNo();
            if(lcNo!=null){
                cell = row.createCell(6);
                //设置样式
                cell.setCellStyle(text(workbook));
                //设置内容
                cell.setCellValue(lcNo);
            }

            //装运港
            String portOfLoading = shipping.getPortOfLoading();
            if(portOfLoading!=null){
                cell = row.createCell(7);
                //设置样式
                cell.setCellStyle(text(workbook));
                //设置内容
                cell.setCellValue(portOfLoading);
            }

            //转运港
            String portOfTrans = shipping.getPortOfTrans();
            if(portOfTrans!=null){
                cell = row.createCell(8);
                //设置样式
                cell.setCellStyle(text(workbook));
                //设置内容
                cell.setCellValue(portOfTrans);
            }

            //卸货港
            String portOfDischar = shipping.getPortOfDischar();
            if(portOfDischar!=null){
                cell = row.createCell(9);
                //设置样式
                cell.setCellStyle(text(workbook));
                //设置内容
                cell.setCellValue(portOfDischar);
            }

            //是否分期
            String isBatch = shipping.getIsBatch();
            if(isBatch!=null){
                cell = row.createCell(10);
                //设置样式
                cell.setCellStyle(text(workbook));
                //设置内容
                cell.setCellValue(isBatch);
            }

            //是否转船
            String isTrans = shipping.getIsTrans();
            if(isTrans!=null){
                cell = row.createCell(11);
                //设置样式
                cell.setCellStyle(text(workbook));
                //设置内容
                cell.setCellValue(isTrans);
            }

            //扼要说明
            String remark = shipping.getRemark();
            if(remark!=null){
                cell = row.createCell(12);
                //设置样式
                cell.setCellStyle(text(workbook));
                //设置内容
                cell.setCellValue(remark);
            }

            //份数
            String copyNum = shipping.getCopyNum();
            if(copyNum!=null){
                cell = row.createCell(13);
                //设置样式
                cell.setCellStyle(text(workbook));
                //设置内容
                cell.setCellValue(copyNum);
            }

            //装期
            Date loadingDate = shipping.getLoadingDate();
            if(loadingDate!=null){
                cell = row.createCell(14);
                //设置样式
                cell.setCellStyle(text(workbook));
                //设置内容
                cell.setCellValue(dateFormat.format(loadingDate));
            }

            //效期
            Date limitDate = shipping.getLimitDate();
            if(limitDate!=null){
                cell = row.createCell(15);
                //设置样式
                cell.setCellStyle(text(workbook));
                //设置内容
                cell.setCellValue(dateFormat.format(limitDate));
            }

            //运输要求
            String specialConditio = shipping.getSpecialConditio();
            if(specialConditio!=null){
                cell = row.createCell(16);
                //设置样式
                cell.setCellStyle(text(workbook));
                //设置内容
                cell.setCellValue(specialConditio);
            }

            //运费说明
            String freight = shipping.getFreight();
            if(freight!=null){
                cell = row.createCell(17);
                //设置样式
                cell.setCellStyle(text(workbook));
                //设置内容
                cell.setCellValue(freight);
            }

            //复核人
            String checkBy = shipping.getCheckBy();
            if(checkBy!=null){
                cell = row.createCell(18);
                //设置样式
                cell.setCellStyle(text(workbook));
                //设置内容
                cell.setCellValue(checkBy);
            }

            //状态
            Integer state = shipping.getState();
            if(state!=null){
                cell = row.createCell(19);
                //设置样式
                cell.setCellStyle(text(workbook));
                //设置内容
                cell.setCellValue(state);
            }

            //创建人
            String createBy = shipping.getCreateBy();
            if(createBy!=null){
                cell = row.createCell(20);
                //设置样式
                cell.setCellStyle(text(workbook));
                //设置内容
                cell.setCellValue(createBy);
            }

            //创建部门
            String createDept = shipping.getCreateDept();
            if(createDept!=null){
                cell = row.createCell(21);
                //设置样式
                cell.setCellStyle(text(workbook));
                //设置内容
                cell.setCellValue(createDept);
            }

            //创建日期
            Date createTime = shipping.getCreateTime();
            if(createTime!=null){
                cell = row.createCell(22);
                //设置样式
                cell.setCellStyle(text(workbook));
                //设置内容
                cell.setCellValue(dateFormat.format(createTime));
            }
        }

        //8. 把工作薄写出
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        //把workbook的内容写出到字节数组汇总
        workbook.write(byteArrayOutputStream);
        new DownloadUtil().download(byteArrayOutputStream,response,"委托单表.xlsx");
    }

    @RequestMapping("/printPdf")
    @ResponseBody
    public void printPdf(String id) throws IOException, JRException {

        //通知浏览器以附件的形式下载
        response.setHeader("content-disposition","attachment;filename=shipping.pdf");

        //1. 得到模板的输入流
        InputStream inputStream = session.getServletContext().getResourceAsStream("/jasper/shipping.jasper");

        //2.把模板与数据填充
        Shipping shipping = shippingService.findById(id);
        Map<String, Object> map = BeanMapUtils.beanToMap(shipping);

        /*//修改isBatch与isTrans的值
        String isBatch = (String) map.get("isBatch");
        if (isBatch.equals("0")) {
            map.put("isBatch","否");
        }else {
            map.put("isBatch","是");
        }
        String isTrans = (String) map.get("isTrans");
        if (isTrans.equals("0")) {
            map.put("0","否");
        }else {
            map.put("1","是");
        }*/

        //把集合封装到数据源中
        JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream,map,new JREmptyDataSource());

        //3. 把pdf文件输出
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }

    //大标题的样式
    public CellStyle bigTitle(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short)16);
        font.setBold(true);//字体加粗
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);				//横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);		//纵向居中
        return style;
    }

    //小标题的样式
    public CellStyle title(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short)12);
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);				//横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);		//纵向居中
        style.setBorderTop(BorderStyle.THIN);						//上细线
        style.setBorderBottom(BorderStyle.THIN);					//下细线
        style.setBorderLeft(BorderStyle.THIN);						//左细线
        style.setBorderRight(BorderStyle.THIN);						//右细线
        return style;
    }

    //文字样式
    public CellStyle text(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("Times New Roman");
        font.setFontHeightInPoints((short)10);

        style.setFont(font);

        style.setAlignment(HorizontalAlignment.LEFT);				//横向居左
        style.setVerticalAlignment(VerticalAlignment.CENTER);		//纵向居中
        style.setBorderTop(BorderStyle.THIN);						//上细线
        style.setBorderBottom(BorderStyle.THIN);					//下细线
        style.setBorderLeft(BorderStyle.THIN);						//左细线
        style.setBorderRight(BorderStyle.THIN);						//右细线

        return style;
    }
}
